package com.lonely.system.engine.upload;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.lonely.common.exception.BusinessException;
import com.lonely.common.utils.oss.PicGoUtil;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * @author : 黄志标
 * @date: 2025/6/27 13:07
 * @description: 基于picgo 实现文件上传
 */
@Component
public class PicGoFileUpload implements FileUploadInterface {

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    @Override
    public String uploadFile(MultipartFile file) {
        String oldFileName = file.getOriginalFilename();
        Assert.isTrue(StrUtil.isNotBlank(oldFileName), "生成的文件的文件名为空");

        String fileName = String.format("%s.%s", IdUtil.randomUUID(), oldFileName.substring(oldFileName.lastIndexOf(".") + 1));
        return PicGoUtil.uploadFile(file, fileName);
    }

    /**
     * 基于输入流上传文件
     *
     * @param inputStream
     * @param fileName
     * @return
     */
    @Override
    public String uploadStream(InputStream inputStream, String fileName) {
        throw new BusinessException("暂未实现");
    }
}
